namespace Collaboris.Saf.Actions.Wss.Tests
{
    using System;

    using Engine.Report;

    using Entities;

    using Generated;

    using Microsoft.VisualStudio.TestTools.UnitTesting;

    using Saf.Tests;

    using Utils;

    using Wss.Site;
    using Wss.WebApplication;

    /// <summary>
    /// This Class (re)creates the http://localhost:4411 and http://localhost:4422
    /// </summary>
    [TestClass]
    public class CreateEnvironments
    {
        #region Public Methods

        /// <summary>
        /// Creates the :4422 site...
        /// </summary>
        [AssemblyInitialize]
        public void CreateBothTestSites()
        {
            ADHelper.DeleteUserAccount("SAFTestAccount");
            ADHelper.CreateUserAccount("SAFTestAccount", "Abcde_1234", "Unit Tester", true);

            this.CreatePublishingSite();
            this.CreatePublishingSiteCollection();

            this.CreateTeamSite();
            this.CreateTeamSiteCollection();
        }

        private void CreateTeamSite()
        {
            WSSTestAssistant<CreateWebApplication, WebApplicationInfo> createwebapplication =
                    new WSSTestAssistant<CreateWebApplication, WebApplicationInfo>();

            using (createwebapplication)
            {
                
                createwebapplication.FirstEntity.AppPoolUserName = Environment.UserDomainName + @"\SAFTestAccount";
                createwebapplication.FirstEntity.AppPoolPassword = "Abcde_1234";
                createwebapplication.FirstEntity.AppPoolId = "Unit Test App Pool Wss";
                createwebapplication.FirstEntity.Port = 4411;
                createwebapplication.FirstEntity.Overwrite = true;
                createwebapplication.FirstEntity.WebAppName = "WSS Site Test Web App";
                createwebapplication.FirstEntity.CreateNewDatabase = true;
                MacroReport doReport = createwebapplication.DoMacro();

            }
        }

        private void CreatePublishingSite()
        {
            WSSTestAssistant<CreateWebApplication, WebApplicationInfo> createwebapplication =
                    new WSSTestAssistant<CreateWebApplication, WebApplicationInfo>();

            using (createwebapplication)
            {

                createwebapplication.FirstEntity.AppPoolUserName = Environment.UserDomainName + @"\SAFTestAccount";
                createwebapplication.FirstEntity.AppPoolPassword = "Abcde_1234";
                createwebapplication.FirstEntity.AppPoolId = "Unit Test App Pool Publishing" ;
                createwebapplication.FirstEntity.Port = 4422;
                createwebapplication.FirstEntity.Overwrite = true;
                createwebapplication.FirstEntity.WebAppName = "Publishing Site Test Web App";
                createwebapplication.FirstEntity.CreateNewDatabase = true;
                MacroReport doReport = createwebapplication.DoMacro();
              
            }
        }

        public void CreateTeamSiteCollection()
        {
            WSSTestAssistant<CreateSite, SiteInfo> createsite =
                    new WSSTestAssistant<CreateSite, SiteInfo>();

            using (createsite)
            {
                createsite.FirstEntity.OwnerName = "Main User";
                createsite.FirstEntity.OwnerLogin = EnvironmentHelper.GetCurrentUserAccount();
                createsite.FirstEntity.OwnerEmail = "noone@collaboris.co.uk";

                // lets change the Entity in place now and see if that works!
                createsite.FirstEntity.SecondaryContactLogin = string.Empty;
                createsite.FirstEntity.SecondaryContactEmail = string.Empty;
                createsite.FirstEntity.SecondaryContactName = string.Empty;
                createsite.FirstEntity.WebTemplate = "STS#1";
                createsite.FirstEntity.WebApplicationUrl = "http://localhost:4411";
                createsite.FirstEntity.SiteCollectionUrl = "/";
                createsite.FirstEntity.Overwrite = true;
                MacroReport doReport = createsite.DoMacro();
            }
        }

        public void CreatePublishingSiteCollection()
        {
            WSSTestAssistant<CreateSite, SiteInfo> createsite =
                    new WSSTestAssistant<CreateSite, SiteInfo>();

            using (createsite)
            {
                createsite.FirstEntity.OwnerName = "Main User";
                createsite.FirstEntity.OwnerLogin = EnvironmentHelper.GetCurrentUserAccount();
                createsite.FirstEntity.OwnerEmail = "noone@collaboris.co.uk";

                // lets change the Entity in place now and see if that works!
                createsite.FirstEntity.SecondaryContactLogin = string.Empty;
                createsite.FirstEntity.SecondaryContactEmail = string.Empty;
                createsite.FirstEntity.SecondaryContactName = string.Empty;
                createsite.FirstEntity.WebTemplate = "BLANKINTERNET#0";
                createsite.FirstEntity.WebApplicationUrl = "http://localhost:4422";
                createsite.FirstEntity.SiteCollectionUrl = "/";
                createsite.FirstEntity.Overwrite = true;
                MacroReport doReport = createsite.DoMacro();
            }
        }

        #endregion
    }
}